package top.aniss.spark.beifeng.dao.impl;

import top.aniss.spark.beifeng.dao.ITaskDAO;
import top.aniss.spark.beifeng.domain.Task;
import top.aniss.spark.beifeng.jdbc.JDBCHelper;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 任务管理DAO实现类
 *
 * @author Andre Wei
 * email aniss.wei@gmail.com
 * create 2018-07-11 下午6:58
 */
public class TaskDAOImpl implements ITaskDAO {
    @Override
    public Task findById(long taskid) {

        final Task task = new Task();

        String sql = "SELECT * FROM task where task_id=?";
        Object[] params = new Object[]{taskid};
        JDBCHelper jdbcHelper = JDBCHelper.getInstance();
        jdbcHelper.executeQuery(sql, params, new JDBCHelper.QueryCallback() {
            @Override
            public void process(ResultSet resultSet) throws SQLException {
                if (resultSet.next()) {
                    int taskid = resultSet.getInt(1);
                    String taskName = resultSet.getString(2);
                    String createTime = resultSet.getString(3);
                    String startTime = resultSet.getString(4);
                    String finishTime = resultSet.getString(5);
                    String taskType = resultSet.getString(6);
                    String taskStatus = resultSet.getString(7);
                    String taskParam = resultSet.getString(8);

                    task.setTaskId(taskid);
                    task.setTaskName(taskName);
                    task.setCreateTime(createTime);
                    task.setStartTime(startTime);
                    task.setFinishTime(finishTime);
                    task.setTaskType(taskType);
                    task.setTaskStatus(taskStatus);
                    task.setTaskParam(taskParam);
                }
            }
        });

        return task;
    }
}
